package com.kaifamiao.www.demo.do0816;
import java.util.*;

public class HashMapDemo {
     static class Person{
        private String name;
        private String gender;
        public Person(String name,String gender){
            super();
            this.gender = gender;
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getGender() {
            return gender;
        }

        public void setGender(String gender) {
            this.gender = gender;
        }
    }
    public static void main(String[] args) {
         //创建对象
        Person person1 = new Person("9527","男");
        Person person2 = new Person("3453","男");
        //创建保存“键 —— 值”对的集合对象
        Map persons = new HashMap();
        //把职业与人物对象按照“键 —— 值”对的方式存储在HashMap中
        persons.put("罪犯",person1);
        persons.put("警察",person2);
//        System.out.println("已添加"+persons.size()+"个人");
//        System.out.println("键集"+persons.keySet());
//        String key = "警察";
//        if (persons.containsKey(key)){
//            Person person = (Person) persons.get(key);
//            System.out.println("职业是"+key +"的人是" + person.getName());
//        }
//        key = "罪犯";
//        if (persons.containsKey(key)){
//            persons.remove(key);
//            System.out.println("犯人"+key + "的信息已被刪除");
//        }

        // 遍历键集   遍历值集
        //输出人物的职业
        System.out.println("[人物的职业]");
//        for (Object key : persons.keySet()){
//            System.out.println(key.toString());
//        }
        Set set = persons.keySet();
        Iterator iterator = set.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }

        System.out.println("[人物的信息]");
//        for (Object value : persons.values()){
//            Person person = (Person)value;
//            System.out.println("姓名"+ person.getName() + ",性别" + person.getGender());
//        }
        Collection collection = persons.values();
        Iterator iterator1 = collection.iterator();
        while (iterator1.hasNext()){

            System.out.println(iterator1.next());
        }
    }
}
